//### BEGIN CALENDAR ### //
(function(){
    var SuperClass = SW.widget.Calendar.AvailabilityCalendar.prototype,
        Calendar = SW.widget.Calendar.Calendar,
        CalUtils = SW.widget.Calendar.Utils,
        Url = SW.tools.Url,
        Html = SW.tools.Html,
        customEvent = YAHOO.util.CustomEvent,
        Content = SW.widget.Calendar.Content;

    function MultiRateCalendar(settings){
        var i, len, selectedIdx = 0;
        this.rateCollection = settings.rateCollection;
        // set initial search params

        //If a rate pref is passed in, find it in the rate collection to use.
        if(settings && settings.ratePrefValue){
            for (i = 0, len = this.rateCollection.length; i < len; i++) {
                if (this.rateCollection[i].ratePrefValue == settings.ratePrefValue) {
                    selectedIdx = i;
                    break;
                }
            }
        }
        else{
            for (i = 0, len = this.rateCollection.length; i < len; i++) {
                if (this.rateCollection[i].addedOnFallback) {
                    continue;
                }
                selectedIdx = i;
                break;
            }
        }

        settings.ratePref = this.rateCollection[selectedIdx].ratePref;
        settings.ratePrefValue = this.rateCollection[selectedIdx].ratePrefValue;
        settings.currencyCode = this.rateCollection[selectedIdx].currencyCode;
        this.rateNotificationArray = [];
        SuperClass.constructor.call(this, settings);
    }
    YAHOO.lang.extend(MultiRateCalendar,SW.widget.Calendar.AvailabilityCalendar,{
        _TYPE:"MultiRateCalendar",
        _CONTAINER_CLASS:"multiRateCalendar",
        initialize:function(){
            SuperClass.initialize.call(this);

            if(this.settings.maxLOSExceeded){
                this.notificationArray[this.notificationArray.length] = this.content.get("ERROR_LOS_EXCEEDED");
            }
        },

        initEvents:function(){
            SuperClass.initEvents.call(this);
          /*  this.beforeApplyAllEvent = new customEvent("beforeApplyAllEvent",null,false,customEvent.FLAT);
            this.applyAllEvent = new customEvent("applyAllEvent",null,false,customEvent.FLAT);*/
        },

        initHandlers:function(){
            SuperClass.initHandlers.call(this);
            this.submitEvent.subscribe(this._continue,this,true);
    /*        this.applyAllEvent.subscribe(this.applyDatesToAllHotels,this,true);*/
        },
       /* toggleHandler:function(e){
            if(e){
                $(e).preventDefault();
            }
            if(this.isShowing()){
                this.hide();
            }
            else{
            	this.show();
            }

        },
*/
        _continue:function(){
            var url = this.fetchRateListAction() || this.settings._baseHref;
            var selectedStay = this.getStayObject();

            //set standard date params
            url = Url.setParameter(url,"arrivalDate",CalUtils.dateToSystemFormat(this.rangeStartDate));
            url = Url.setParameter(url,"departureDate",CalUtils.dateToSystemFormat(this.rangeEndDate));
            url = Url.setParameter(url,"lengthOfStay",CalUtils.getLengthOfStay(this.rangeStartDate,this.rangeEndDate));

            //set lpq rate params if not award booking
            if (!selectedStay.pts) {
                url = Url.setParameter(url,"lpqRatePlanName",selectedStay.rp);
                url = Url.setParameter(url,"lpqRate",selectedStay.rate);
            }

            if(selectedStay.curr)
            {
                url = Url.setParameter(url,"currencyCode",selectedStay.curr);
            }
            document.location.href = url;
        },

        /*_applyDates:function(){
            if(this.beforeApplyAllEvent.fire()){
                this.applyAllEvent.fire();
            }
        },*/

        parseDataError:function(){
            this.render();
            this.parseError();
        },

        fetchRateListAction:function(){
            var ratePlanKey = this.getRatePlanKey();
            var matchKey;
            for(var i=0; i<this.rateCollection.length; i++){
                matchKey = this.rateCollection[i].ratePref + '_' + this.rateCollection[i].ratePrefValue;
                if(matchKey == ratePlanKey){
                    return this.rateCollection[i].rateListUrl;
                }
            }
        },

        createLegend:function(container){
            var legend = document.createElement("li");
            $(legend).addClass("legendContainer");
            container.appendChild(legend);

            var ratePlanContainer = document.createElement("div");
            var ratePlanSelector = document.createElement("select");
            ratePlanContainer.appendChild(ratePlanSelector)

            $(ratePlanSelector).on("change",function(calendar) {
                return function(e) { calendar.selectNewRate(e) };
            }(this));

            var ratePlanKey = this.getRatePlanKey();
            var ratePlanElement, textNode, optionItem, radioLabel, matchKey;

            for(var i=0; i<this.rateCollection.length; i++){
                matchKey = this.rateCollection[i].ratePref + '_' + this.rateCollection[i].ratePrefValue;

                optionItem = document.createElement('option');

                optionItem.setAttribute('value',matchKey);
                textNode = document.createTextNode(this.rateCollection[i].ratePrefLabel);
                optionItem.appendChild(textNode);

                if(matchKey == ratePlanKey){
                    optionItem.setAttribute("selected", "selected");
                }

                ratePlanSelector.appendChild(optionItem);

            }
            legend.appendChild(ratePlanContainer);
        },
        createFooter: function(container) {
            // create menuHolder Div
            var menuHolder = document.createElement("div");
            $(menuHolder).addClass("messageContainer");
            container.appendChild(menuHolder);
            this._menuElements.menuHolder = menuHolder;

            // create error display
            var errorContainer = document.createElement("ul");
            $(errorContainer).addClass("errorContainer");
            $(errorContainer).addClass("errorCallout");
            menuHolder.appendChild(errorContainer);
            this._menuElements.errorContainer = errorContainer;

            var instructions = document.createElement("ul");
            $(instructions).addClass("instructions");
            /*var checkInInstructions = document.createElement("li");
            $(checkInInstructions).addClass("rangeStartInstructions");
            this._menuElements.rangeStartInstructions = checkInInstructions;*/

            var checkInInstructions = document.createElement("li");
            var checkInInstructionsLabel = document.createElement("div");
            $(checkInInstructionsLabel).addClass("checkInInstructionsLabel");
            checkInInstructionsLabel.innerHTML = this.content.get("RANGE_START_PREFIX");
            var checkInInstructionsDate = document.createElement("div");
            $(checkInInstructionsDate).addClass("checkInInstructionsDate");
            $(checkInInstructions).addClass("rangeEndInstructions");
            this._menuElements.rangeStartInstructions = checkInInstructions;
            this._menuElements.rangeStartInstructionsDate = checkInInstructionsDate;
            checkInInstructions.appendChild(checkInInstructionsLabel);
            checkInInstructions.appendChild(checkInInstructionsDate);

            var clearDiv = document.createElement("div");
            $(clearDiv).addClass("clearDiv");

            checkInInstructions.appendChild(clearDiv);

            var checkOutInstructions = document.createElement("li");
            var checkOutInstructionsLabel = document.createElement("div");
            $(checkOutInstructionsLabel).addClass("checkOutInstructionsLabel");
            checkOutInstructionsLabel.innerHTML = this.content.get("RANGE_END_PREFIX");
            var checkOutInstructionsDate = document.createElement("div");
            $(checkOutInstructionsDate).addClass("checkOutInstructionsDate");
            $(checkOutInstructions).addClass("rangeEndInstructions");
            this._menuElements.rangeEndInstructions = checkOutInstructions;
            this._menuElements.rangeEndInstructionsDate = checkOutInstructionsDate;
            checkOutInstructions.appendChild(checkOutInstructionsLabel);
            checkOutInstructions.appendChild(checkOutInstructionsDate);

            var clearDiv = document.createElement("div");
            $(clearDiv).addClass("clearDiv");

            checkOutInstructions.appendChild(clearDiv);
            instructions.appendChild(checkInInstructions);
            instructions.appendChild(checkOutInstructions);
            menuHolder.appendChild(instructions);
            var clearDiv = document.createElement("div");
            $(clearDiv).addClass("clearDiv");

            var rateSummaryContainer = document.createElement("li");
            this._menuElements.rateSummaryContainer = rateSummaryContainer;
            instructions.appendChild(rateSummaryContainer);

            menuHolder.appendChild(clearDiv);

            var optionalInstructionsContainer = document.createElement("div");
            $(optionalInstructionsContainer).addClass("optionalInstructions");
            this._menuElements.optionalInstructionsContainer = optionalInstructionsContainer;
            menuHolder.appendChild(optionalInstructionsContainer);

            this.createLegend(instructions);

        },

        selectNewRate:function(e){
            var selectedValue = $(e.target).val().split("_");
            this.reset();
            this.setRatePlanKey(selectedValue[0], selectedValue[1]);
            this.verifyRateMessages();
            this.getData();
        },

        verifyRateMessages:function(){
            this.rateNotificationArray = [];
            var ratePlanKey = this.getRatePlanKey();
            var matchKey;

            if(this.notification) {
                this.rateNotificationArray.push(this.notification);
            }
            for(var i=0; i<this.rateCollection.length; i++){
                matchKey = this.rateCollection[i].ratePref + '_' + this.rateCollection[i].ratePrefValue;
                if(matchKey == ratePlanKey){
                    if(this.rateCollection[i].isFreeNightAward && this.rateCollection[i].noFreeNightMessage){
                        this.rateNotificationArray.push(this.rateCollection[i].noFreeNightMessage);
                        break;
                    }
                }
            }
        },

        renderErrors:function(){
            //notifications are similar to errors except they persist and aren't cleared
            var output = []
            if(this.notificationArray.length > 0){
                for(var i=0; i<this.notificationArray.length; i++){
                    output.push('<li>' + this.notificationArray[i] +'</li>');
                }
            }
            if(this.rateNotificationArray.length > 0){
                for(var i=0; i<this.rateNotificationArray.length; i++){
                    output.push('<li>' + this.rateNotificationArray[i] +'</li>');
                }
            }
            if(this.errorArray.length > 0){
                for(var j=0; j<this.errorArray.length; j++){
                    output.push('<li>' + this.errorArray[j] +'</li>');
                }
            }
            if(output.length > 0){
                this._menuElements.errorContainer.innerHTML = output.join("\n");
                $(this._menuElements.errorContainer).addClass("show");
            }else{
                $(this._menuElements.errorContainer).removeClass("show");
                this._menuElements.errorContainer.innerHTML = "";
            }
            this.errorArray = [];
        }
    });

    SW.widget.Calendar.MultiRateCalendar = MultiRateCalendar;
})();